package com.sl.biz.modular.group.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.biz.modular.group.controller.domain.request.SquareLakeGroupAddRequest;
import com.sl.biz.modular.group.controller.domain.request.SquareLakeGroupDeleteByIdRequest;
import com.sl.biz.modular.group.controller.domain.request.SquareLakeGroupGetByIdRequest;
import com.sl.biz.modular.group.controller.domain.request.SquareLakeGroupUpdateRequest;
import com.sl.biz.modular.group.dao.entity.GroupDO;
import com.sl.biz.modular.group.dao.enums.GroupRelationTypeCodeEnum;
import com.sl.biz.modular.group.dao.service.GroupDaoService;
import com.sl.biz.modular.group.domain.dto.BizSquareLakeGroupDTO;
import com.sl.biz.modular.group.service.SquareLakeGroupBizService;
import com.sl.core.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class SquareLakeGroupBizServiceImpl implements SquareLakeGroupBizService {
//    ProcessRepositoryManager processRepositoryManager = ProcessEngine.INSTANCE.repositoryManager();

    private final GroupDaoService groupDaoService;

    public static final String ALL_GROUP_CODE = "ALL";

    @Override
    public List<Tree<String>> tree(String relationTypeCode, String productId) {
        if (StrUtil.isBlank(relationTypeCode)) {
            relationTypeCode = GroupRelationTypeCodeEnum.PROCESS.getCode();
        }
        List<GroupDO> groupDOS = groupDaoService.queryByRelationTypeCode(relationTypeCode,productId);
        GroupDO groupDO = new GroupDO();
        groupDO.setGroupCode("ALL");
        groupDO.setGroupName("全部");
        groupDO.setDescription("全部");
//        groupDO.setRelationId("");
        groupDO.setRelationTypeCode(relationTypeCode);
        groupDO.setTreeParentId("-1");
        groupDO.setTreeLevel(0);
        groupDO.setTreeLeafFlag(0);
        groupDO.setTreePath("-1>0");
        groupDO.setId("0");
        groupDOS.add(groupDO);

        List<TreeNode<String>> collect = groupDOS.stream().map(v -> {
            TreeNode<String> treeNode = new TreeNode<>();
            treeNode.setId(v.getId());
            treeNode.setName(v.getGroupName());
            treeNode.setParentId(v.getTreeParentId());
            treeNode.setExtra(JsonUtil.toMap(v));
            return treeNode;
        }).collect(Collectors.toList());
        //TreeNode
        //树路径构建
        List<Tree<String>> build = TreeUtil.build(collect, "-1");
        return build;
    }

    @Override
    public String add(SquareLakeGroupAddRequest addRequest) {

        String id = IdUtil.getSnowflakeNextIdStr();
        GroupDO groupDO = new GroupDO();
        groupDO.setGroupCode(addRequest.getCode());
        groupDO.setGroupName(addRequest.getName());
        groupDO.setDescription(addRequest.getName());

        String relationTypeCode = addRequest.getRelationTypeCode();
        if (StrUtil.isBlank(relationTypeCode)) {
            relationTypeCode = GroupRelationTypeCodeEnum.PROCESS.getCode();
        }
        groupDO.setRelationTypeCode(relationTypeCode);
        groupDO.setProductId(addRequest.getProductId());
        String parentId = addRequest.getParentId();
        groupDO.setTreeParentId(parentId);

        if (StrUtil.isNotBlank(parentId) && !StrUtil.equals(parentId, "0")) {
            GroupDO parentGroupDO = groupDaoService.queryById(addRequest.getParentId());
            groupDO.setTreeLevel(parentGroupDO.getTreeLevel() + 1);
            groupDO.setTreeLeafFlag(1);
            groupDO.setTreePath(parentGroupDO.getTreePath() + ">" + id);
        } else {
            groupDO.setTreeLevel(1);
            groupDO.setTreeLeafFlag(1);
            groupDO.setTreePath("0>" + id);
            groupDO.setTreeParentId("0");
        }

        groupDO.setId(id);
         groupDaoService.add(groupDO);
         return id;
    }

    @Override
    public Boolean updateById(SquareLakeGroupUpdateRequest addRequest) {
        GroupDO groupDO = new GroupDO();
        groupDO.setGroupCode(addRequest.getCode());
        groupDO.setGroupName(addRequest.getName());
        groupDO.setId(addRequest.getId());
        return groupDaoService.updateById(groupDO);
    }

    @Override
    public Boolean deleteById(SquareLakeGroupDeleteByIdRequest addRequest) {

        //判断是否存在叶子接点如果存在，不能删除
        GroupDO groupDO = groupDaoService.queryById(addRequest.getId());
        if (ObjectUtil.isNull(groupDO)) {
            return false;
        }
        List<GroupDO> groupDOS = groupDaoService.queryByTreePath(groupDO.getTreePath());

        long count = groupDOS.stream().filter(v -> !StrUtil.equals(v.getId(), addRequest.getId())).count();

        Assert.isFalse(count > 0, "存在子节点，不能删除");
        return groupDaoService.removeById(addRequest.getId());
    }

    @Override
    public BizSquareLakeGroupDTO getById(SquareLakeGroupGetByIdRequest addRequest) {
//        SquareLakeGroupDTO byId = processRepositoryManager.directory().getById(addRequest.getId());
        String id = addRequest.getId();
        if (StrUtil.equals(id, "0")) {
            return new BizSquareLakeGroupDTO();
        }
        GroupDO byId = groupDaoService.getById(addRequest.getId());
        Assert.isTrue(ObjectUtil.isNotNull(byId), "获取分组信息失败");

        BizSquareLakeGroupDTO bizSquareLakeGroupDTO = new BizSquareLakeGroupDTO();
        bizSquareLakeGroupDTO.setName(byId.getGroupName());
        bizSquareLakeGroupDTO.setCode(byId.getGroupCode());
        bizSquareLakeGroupDTO.setId(byId.getId());
        return bizSquareLakeGroupDTO;
    }
}
